package vip.xiaozhao.intern.baseUtil.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import vip.xiaozhao.intern.baseUtil.controller.util.IPUtil;
import vip.xiaozhao.intern.baseUtil.intf.dto.ResponseDO;
import vip.xiaozhao.intern.baseUtil.intf.entity.User;
import vip.xiaozhao.intern.baseUtil.intf.mapper.UserMapper;
import vip.xiaozhao.intern.baseUtil.intf.utils.JjwtUtil;
import vip.xiaozhao.intern.baseUtil.intf.utils.security.MD5Signature;


import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/")
@Slf4j
public class HomeController {
    @Autowired
    UserMapper userMapper;

    @RequestMapping("/hello")
    public String init(HttpServletRequest request) {
        log.info("show hello");
        return "hello";
    }

    //登录接口
    @PostMapping("/login")
    public ResponseDO login(@RequestBody User user) throws Exception {
        //判断用户名与密码是否为空
        if (user.getUserName() == null || user.getUserName().equals("") || user.getPassword() == null || user.getPassword().equals("")) {
            return ResponseDO.fail("用户名或密码为空");
        }
        //判断密码是否正确
        int userId = userMapper.selectUserId(user.getUserName(), user.getPassword());

        String loginToken = JjwtUtil.getLoginToken(user.getId());
        return ResponseDO.success(loginToken);
    }

    //根据id查询用户数据
    @GetMapping("/login/{id}")
    public ResponseDO loginById(@PathVariable int id) throws Exception {
        User user = userMapper.selectUserById(id);
        return ResponseDO.success(user);
    }

    //插入用户信息
    @PostMapping("/insertUser")
    public ResponseDO insertUser(@RequestBody User user) {
        userMapper.insertUser(user);
        log.info("插入用户信息成功");
        return ResponseDO.success("插入用户信息成功");
    }
    //更改用户信息
    @PostMapping("/updateUser")
    public ResponseDO updateUser(@RequestBody User user) {
        userMapper.updateUserById(user);
        log.info("更改用户信息成功");
        return ResponseDO.success("更改用户信息成功");
    }


}
